//
//  WorkingVC.m
//  hnGovernment
//
//  Created by 安仔 on 17/2/24.
//  Copyright © 2017年 安仔. All rights reserved.
//

#import "WorkingVC.h"
#import "WorkingCell.h"
#import "DTExhibitionModel.h"
#import "SserviceByCategoryObj.h"
#import "DTWebViewController.h"
@interface WorkingVC ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UISegmentedControl *mySegment;//分段器
@property (nonatomic, strong) UISearchBar *workingSearchBar;//搜索框
@property(nonatomic, strong) UICollectionView *collectView;//微平台
@property (nonatomic, strong) NSArray *collectArray;
@property (nonatomic,strong) NSArray *enhibitionModelArray;// 表格展台模型数据

@end

@implementation WorkingVC
{
    NSString *exhibitionCategoryId;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //初始化界面
    [self createWorkingView];
    
    //请求数据，默认3
    exhibitionCategoryId = @"3";
    [self requestSelectServiceByExhibitionCategoryId:exhibitionCategoryId];
}

#pragma mark- action
-(void)didClicksegmentedControlAction:(UISegmentedControl *)Seg {
    NSInteger Index = Seg.selectedSegmentIndex;
    switch (Index) {
        case 0:
            exhibitionCategoryId = @"3";
            [self requestSelectServiceByExhibitionCategoryId:exhibitionCategoryId];
            break;
        case 1:
            exhibitionCategoryId = @"4";
            [self requestSelectServiceByExhibitionCategoryId:exhibitionCategoryId];
            break;
        case 2:
            exhibitionCategoryId = @"5";
            [self requestSelectServiceByExhibitionCategoryId:exhibitionCategoryId];
            break;
        default:
            break;
    }
}

//根据展台分类查询应用
- (void) requestSelectServiceByExhibitionCategoryId:(NSString *)str {
    
    __weak __typeof(self) weakSelf = self;
    [DDNetWorkManager exhibitionCategoryId:str cityId:@"330102" controller:weakSelf success:^(id result, DDcacheType cacheType, DDresultType resultType) {
        
        [DDHudHelper showHudWithResult:result resultSuccess:^{
            //NSLog(@"%@",result);
            _enhibitionModelArray = [DTExhibitionModel objectArrayWithKeyValuesArray:result[@"data"]];
            DTExhibitionModel *obj = _enhibitionModelArray.firstObject;
            _collectArray = obj.exhibitionService;
            [_collectView reloadData];
        }];
       
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:NSLocalString(@"网络错误")];
    }];
}

#pragma mark- collectionView delegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    SserviceByCategoryObj *obj = self.collectArray[indexPath.item];
    DTWebViewController *webViewController = [[DTWebViewController alloc]init];
    webViewController.webViewUrlString = obj.url;
    webViewController.title = obj.serviceName;
    self.navigationController.navigationBar.hidden = NO;
    [self.navigationController pushViewController:webViewController animated:YES];
}

#pragma mark- 初始化界面
- (void)createWorkingView {
    //导航背景
    UIView *narView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 128*m6Scale)];
    narView.backgroundColor = APPColor;
    [self.view addSubview:narView];
    
    //分段器
    NSArray *segmentArr = @[@"个人服务", @"法人服务", @"部门服务"];
    self.mySegment = [[UISegmentedControl alloc] initWithItems:segmentArr];
    self.mySegment.selectedSegmentIndex = 0;
    self.mySegment.tintColor = [UIColor whiteColor];
    [self.mySegment addTarget:self action:@selector(didClicksegmentedControlAction:)forControlEvents:UIControlEventValueChanged];
    [narView addSubview:self.mySegment];
    [self.mySegment mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(narView.mas_centerX);
        make.bottom.mas_equalTo(narView.mas_bottom).offset(-20*m6Scale);
        make.size.mas_equalTo(CGSizeMake(710*m6Scale, 60*m6Scale));
    }];
    
    //搜索框背景
    UIView *markView = [[UIView alloc] initWithFrame:CGRectMake(0, 128*m6Scale, KScreenWidth, 82*m6Scale)];
    markView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:markView];
    
    //搜索框
    self.workingSearchBar = [[UISearchBar alloc] init];
    self.workingSearchBar.placeholder = NSLocalString(@"郑州出台房屋征收与补偿方法");
    self.workingSearchBar.barStyle = UIBarStyleDefault;
    self.workingSearchBar.backgroundColor = [UIColor colorWithHexString:@"eeeeee"];
    self.workingSearchBar.layer.cornerRadius = 8*m6Scale;
    self.workingSearchBar.clipsToBounds = YES;
    [[[[self.workingSearchBar.subviews objectAtIndex : 0 ] subviews ] objectAtIndex : 0 ] removeFromSuperview ];
    self.workingSearchBar.subviews.firstObject.subviews.firstObject.subviews.firstObject.hidden = YES;
    [self.view addSubview:self.workingSearchBar];
    [self.workingSearchBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(148*m6Scale);
        make.size.mas_equalTo(CGSizeMake(710*m6Scale, 64*m6Scale));
    }];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.collectView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 210*m6Scale, KScreenWidth, KScreenHeight-(210+96)*m6Scale) collectionViewLayout:layout];
    self.collectView.delegate = self;
    self.collectView.dataSource = self;
    self.collectView.showsVerticalScrollIndicator = YES;
    self.collectView.showsHorizontalScrollIndicator = NO;
    self.collectView.backgroundColor = [UIColor whiteColor];
    //注册 colllectionCell
    [self.collectView registerClass:[WorkingCell class] forCellWithReuseIdentifier:@"WorkingCell"];
    [self.view addSubview:self.collectView];
}

#pragma mark- UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.collectArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    WorkingCell *cell = [self.collectView dequeueReusableCellWithReuseIdentifier:@"WorkingCell" forIndexPath:indexPath];
    SserviceByCategoryObj *obj = self.collectArray[indexPath.item];
    cell.titleLab.text = obj.serviceName;
    [cell.signImg sd_setImageWithURL:[NSURL URLWithString:obj.serviceImg]];
    return cell;
}

#pragma mark ---- UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(KScreenWidth / 2, 142*m6Scale);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
